package com.rslai.base.tool.servertest;

import com.rslai.base.tool.servertest.model.KeyValueStore;
import com.rslai.base.tool.servertest.transport.http.HttpService;
import com.rslai.base.tool.servertest.util.PropertyUtils;

import java.util.Arrays;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class MonitorLog {
    protected static final Logger logger = LoggerFactory.getLogger(MonitorLog.class);

    public static void start(ServertestOptions options) {
        try {
            if (!isValid(options)) return;
            HttpService.get("http://autotest.corp.qunar.com/start", buildParameters(options));
        } catch (Exception e) {
            logger.error("Start log monitor error", e);
        }
    }

    private static List<KeyValueStore> buildParameters(ServertestOptions options) {
        String autotestlog = PropertyUtils.getProperty("autotest-log.host");
        KeyValueStore job = new KeyValueStore("job", options.jobName());
        KeyValueStore build = new KeyValueStore("build", options.buildNumber());
        KeyValueStore list = new KeyValueStore("list", autotestlog);
        return Arrays.asList(new KeyValueStore[]{job, build, list});
    }

    private static boolean isValid(ServertestOptions options) {
        String autotestlog = PropertyUtils.getProperty("autotest-log.host");
        if ((StringUtils.isBlank(autotestlog)) || (StringUtils.isBlank(options.jobName())) || (StringUtils.isBlank(options.buildNumber()))) {
            return false;
        }
        return true;
    }
}
